package cn.wen.javax.servlet.http;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import cn.wen.bases.framework.struts.UtilsOfStruts;

/**
 * Title：响应相关
 * <p>
 * Description：辅助工具。
 * <p>
 * Copyright：Copyright (c) 2010
 * 
 * @author <a href="mailto:ct_wen@qq.com">ctwen</a>, 温承瑭
 * @version Revision：1.0 Date：2011/11/18 Description：初完成。
 *          <p>
 */
public final class UtilsOfResponse {

    private static Logger logger = Logger.getLogger(UtilsOfResponse.class);

    /**
     * <pre>
     * 获取（当前）HttpServletResponse
     * 
     * PS：该方法需要Struts2。
     * <pre>
     * 
     * @return HttpServletResponse
     */
    public static HttpServletResponse getResponse() {
	return UtilsOfStruts.getHttpServletResponse();
    }

    public static void setHeaderByNoCache(HttpServletResponse response) {
	response.setHeader("Cache-Control", "no-cache,must-revalidate");
	response.setHeader("Pragma", "no-cache");
	response.setDateHeader("Expires", 0L);
    }

    /**
     * <pre>
     * Set the response headers to download. 
     * PS:attachment to.
     * </pre>
     * 
     * @param response HttpServletResponse.
     * @param filename file name.
     * @throws UnsupportedEncodingException Filename encoding conversion exception.
     * 
     * @see {@link #setHeaderByDownload(HttpServletResponse, String, boolean)}
     */
    public static void setHeaderByDownload(HttpServletResponse response, String filename) throws UnsupportedEncodingException {
	setHeaderByDownload(response, filename, false);
    }

    /**
     * <pre>
     * Set the response headers to download. 
     * </pre>
     * 
     * @param response HttpServletResponse.
     * @param filename file name.
     * @param isInline Online or attachment to.
     * @throws UnsupportedEncodingException Filename encoding conversion exception.
     * 
     * @see {@link #setHeaderByDownload(HttpServletResponse, String)}
     */
    public static void setHeaderByDownload(HttpServletResponse response, String filename, boolean isInline)
	    throws UnsupportedEncodingException {
	String str = null;

	str = MimeMapping.getContentType(filename) + ";charset=GBK";
	response.setContentType(str);
	str = (isInline ? "inline" : "attachment") + ";filename=" + new String(filename.getBytes("GB2312"), "ISO8859-1");
	response.setHeader("Content-disposition", str);
    }

    /**
     * @see #sendAs(HttpServletResponse, String, String)
     */
    public static void sendAsJson(HttpServletResponse response, String str) {
	sendAs(response, str, "json");
    }
    /**
     * @see #sendAs(HttpServletResponse, String, String)
     */
    public static void sendAsText(HttpServletResponse response, String str) {
	sendAs(response, str, "html");
    }

    /**
     * 按指定文档类型将内容写入HttpServletResponse.
     * @param response HttpServletResponse
     * @param str 内容
     * @param mime 文档类型(请参考：MimeMapping)
     * @see cn.wen.javax.servlet.http.MimeMapping
     */
    public static void sendAs(HttpServletResponse response, String str, String mime) {
	response.setContentType(MimeMapping.getContentType(mime)+";charset=UTF-8");
	if (null != str) try {
	    response.getWriter().write(str);
	}
	catch (IOException e) {
	    logger.error(e.getMessage(), e);
	}
    }

}
